home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MyDeviceLoop.c
-
- Contains: This snippet shows how to write a device loop that
- works under System 7 and pre-7.0 systems. As
- described on pages 21-23 and 21-24 of Inside Mac
- volume VI, a device loop procedure searches all
- active screen devices, calling a drawing procedure
- whenever it encounters a screen that intersects
- the drawing region. In this app the drawing
- region is the app's window bounds and the chosen
- drawing procedure simply displays the screen's
- colors for every device the window bounds intersect.
-
- Written by: Edgar Lee
-
- Copyright: Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 7/12/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
-
-
- */
-
- #include <Dialogs.h>
- #include <Fonts.h>
-
- /* Constant Declarations */
-
- #define WWIDTH 400
- #define WHEIGHT 256
-
- #define WLEFT (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
- #define WTOP (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
-
-
- enum PointSelector {topLeft,
- botRight};
- /* Global Variable Definitions */
-
- WindowPtr gWindow;
-
- void initMac();
- void createWindow();
- void doMyDeviceLoop();
- void doDraw();
- void doEventLoop();
-
- void main(void)
- {
- initMac();
-
- createWindow();
-
- doEventLoop();
- }
-
- void initMac()
- {
- MaxApplZone();
-
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( nil );
- InitCursor();
- FlushEvents( 0, everyEvent );
- }
-
- void createWindow()
- {
- Rect rect;
-
- SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
- gWindow = NewCWindow( 0L, &rect, "\pMyDeviceLoop", true, documentProc,
- (WindowPtr)-1L, true, 0L );
- SetPort( gWindow );
-
- TextFont( kFontIDTimes );
- TextSize( 48 );
- TextMode( srcCopy );
- }
-
- void doMyDeviceLoop()
- {
- int depth;
- Rect gDeviceRect;
- Rect intersectingRect;
- GDHandle gDevice;
- //Point point;
-
- /* Get the handle to the first device in the list. */
- gDevice = GetDeviceList();
-
- /* Loop through all the devices in the list. */
- while (gDevice != nil)
- {
- /* Get the device's gdRect and convert it to local coordinates. */
- gDeviceRect = (**gDevice).gdRect;
- depth = (**(**gDevice).gdPMap).pixelSize;
-
- GlobalToLocal( topLeft );
- GlobalToLocal( (Point *)botRight );
-
- /* Check if the app's window rect intersects the device's, and if it */
- /* does, set the clip region's rect to the intersection, then DRAW! */
-
- if (SectRect( &gWindow->portRect, &gDeviceRect, &intersectingRect ))
- {
- ClipRect( &intersectingRect );
- doDraw( depth, &intersectingRect );
- }
-
- /* Get the next device in the list. */
- gDevice = GetNextDevice( gDevice );
- }
- }
-
- void doDraw( depth)
- int depth;
- {
- int i;
- int totalColors;
- int penThickness;
- //RGBColor color;
- CTabHandle ctable;
- Str255 string = "\pDirect Colors";
-
- if (depth > 8)
- {
- BackColor( blackColor );
- ForeColor( blueColor );
-
- /* Draw text for direct colors mode. */
- EraseRect( &gWindow->portRect );
- MoveTo( (gWindow->portRect.right - StringWidth( string )) / 2, 130 );
- DrawString( string );
- }
- else
- {
- /* Get the colortable at this depth. */
- ctable = GetCTable( depth );
-
- /* Set the line thickness to a fraction of the window height. */
- totalColors = (2 << depth) / 2;
- penThickness = gWindow->portRect.bottom / totalColors;
- PenSize( 1, penThickness );
-
- /* Now draw the colors at this depth. */
- for (i = 0; i < totalColors; i++)
- {
- RGBForeColor( &(**ctable).ctTable[i].rgb );
- MoveTo( 0, i * penThickness );
- LineTo( gWindow->portRect.right, i * penThickness );
- }
-
- /* Release the colortable memory. */
- DisposeCTable( ctable );
- }
- }
-
- void doEventLoop()
- {
- EventRecord event;
- WindowPtr window;
- short clickArea;
- Rect screenRect;
-
- for (;;)
- {
- if (WaitNextEvent( everyEvent, &event, 0, nil ))
- {
- if (event.what == mouseDown)
- {
- clickArea = FindWindow( event.where, &window );
-
- if (clickArea == inDrag)
- {
- screenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow( window, event.where, &screenRect );
- }
- else if (clickArea == inContent)
- {
- if (window != FrontWindow())
- SelectWindow( window );
- }
- else if (clickArea == inGoAway)
- if (TrackGoAway( window, event.where ))
- return;
- }
- else if (event.what == updateEvt)
- {
- window = (WindowPtr)event.message;
- SetPort( window );
-
- BeginUpdate( window );
- doMyDeviceLoop();
- EndUpdate( window );
- }
- }
- }
- }